package com.urun.interceptor.hotanalysis.mapper.db1;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.urun.interceptor.hotanalysis.DTO.StWordHiddenDTO;
import com.urun.interceptor.hotanalysis.DTO.StWordSearchDTO;
import com.urun.interceptor.hotanalysis.DTO.StWordSortDTO;
import com.urun.interceptor.hotanalysis.bean.StWords;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface StWordsMapper extends BaseMapper<StWords> {

    /**
     * 通过Id查询表里面的所有字段
     * @param Id
     * @return
     */
    @Select("select * from st_words where Id =#{id}")
     List<StWords>selectWordById(Integer Id);

    /**
     * 通过词频来进行排行
     * @return
     */
    @Select(" SELECT (@i := @i + 1) AS RowNum,Frequency,(CASE WHEN NewWord IS NULL THEN Word WHEN NewWord = '' THEN Word ELSE NewWord END) AS 'RealWord' FROM st_words,(SELECT @i := 0) AS it ORDER BY Frequency DESC LIMIT 20;")
    List<StWordSortDTO>SortFrequency();


    /**
     * 搜索的是具有Id;Word;Status;RealWord的字段
     * 搜索展示出来的是当前热词
     * @return
     */
    @Select("SELECT id, Frequency,`Status`,(CASE WHEN NewWord IS NULL THEN Word WHEN NewWord = '' THEN Word ELSE NewWord END) AS 'realWord' FROM st_words WHERE `Status` = 1 ORDER BY Frequency DESC LIMIT 20;")
     List<StWordSearchDTO> SearchWordDTO();

    /**
     * 搜索出来的是被屏蔽的热词
     * @return
     */
    @Select("SELECT id, Frequency,`Status`,(CASE WHEN NewWord IS NULL THEN Word WHEN NewWord = '' THEN Word ELSE NewWord END) AS 'realWord' FROM st_words WHERE `Status` = 2 ORDER BY Frequency DESC LIMIT 20;\n")
     List<StWordSearchDTO> SearchHiddenWordDTO();


    /**
     * 通过id修改当前的NewWord
     * @param newWord
     * @param id
     * @return
     */
    @Insert("update st_words set NewWord=#{newWord} where Id=#{id}")
     boolean UpdateNewWord(String newWord,Integer id);


    /**
     * 通过当前热词的Id来修改Status
     */
    @Update("UPDATE st_words SET `Status` = (CASE WHEN `Status` = 1 THEN 2 WHEN `Status` = 2 THEN 1 ELSE  1 END) WHERE Id =#{id};")
    boolean UpdateStatus(Integer id);


    /**
     * 恢复默认键 通过修改全部Status恢复为默认1
     */
    @Update("UPDATE st_words SET `Status`=1;")
    boolean UpdateDefaultStatus();


    @Select("SELECT id, Word,`Status`,NewWord FROM st_words WHERE `Status` = 1 LIMIT 20;")
    List<StWordHiddenDTO> search();
}
